package com.jingtuo.android.common.os;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference;

import com.jingtuo.android.common.CacheManager;
import com.jingtuo.android.common.utils.HttpUtils;
import com.jingtuo.android.common.utils.ImageUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;

public class AsyncBitmap extends AsyncTask<Void, Void, Bitmap> {
	
	private Context context;
	
    private final WeakReference<ImageView> imageViewReference;
    
    private CacheManager cacheManager;
    
    private BitmapInfo bitmapInfo;
	
	public AsyncBitmap(CacheManager cacheManager, ImageView imageView, BitmapInfo bitmapInfo){
		this.context = imageView.getContext();
		this.cacheManager = cacheManager;
		this.imageViewReference = new WeakReference<ImageView>(imageView);
		this.bitmapInfo = bitmapInfo;
	}
	
	@Override
	protected void onPostExecute(Bitmap result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		if(isCancelled()){
			result = null;
		}
		if(imageViewReference!=null&&result!=null){
			ImageView imageView = imageViewReference.get();
			if(imageView!=null){
				if(bitmapInfo.getType()==BitmapInfo.TYPE_RES){
					
				}else if(bitmapInfo.getType()==BitmapInfo.TYPE_ASSETS){
					
				}else if(bitmapInfo.getType()==BitmapInfo.TYPE_FILE){
					cacheManager.put(bitmapInfo.getPath(), result);
				}else if(bitmapInfo.getType()==BitmapInfo.TYPE_URL){
					
				}
				imageView.setImageBitmap(result);
			}
		}
	}

	@Override
	protected Bitmap doInBackground(Void... params) {
		Bitmap bitmap = null;
		if(bitmapInfo!=null){
			String path = bitmapInfo.getPath();
			if(path!=null&&!path.equals("")){
				if(BitmapInfo.TYPE_RES.equals(bitmapInfo.getType())){
					
				}else if(BitmapInfo.TYPE_ASSETS.equals(bitmapInfo.getType())){
					
				}else if(BitmapInfo.TYPE_FILE.equals(bitmapInfo.getType())){
					if(bitmapInfo.isIcon()){
						bitmap = ImageUtils.decodeIcon(context, bitmapInfo.getPath(), bitmapInfo.getIconSize());
					}else{
						bitmap = ImageUtils.decodeFile(context, path, bitmapInfo.getDstWidth());
					}
				}else if(BitmapInfo.TYPE_URL.equals(bitmapInfo.getType())){
					if(bitmap==null){
						String name = "";
						int start = path.lastIndexOf("/");
						if(start!=-1){
							name = path.substring(start + 1);
						}
						if(cacheManager.getDiskCacheDir()!=null){//磁盘缓存可用
							File file = cacheManager.getCacheFile(name);
							if(file!=null){
								if(bitmapInfo.isIcon()){
									bitmap = ImageUtils.decodeIcon(context, file.getAbsolutePath(), bitmapInfo.getDstWidth());
								}else{
									bitmap = ImageUtils.decodeFile(context, file.getAbsolutePath(), bitmapInfo.getDstWidth());
								}
							}else{
								bitmap = HttpUtils.getBitmap(path);
								try {
									if(bitmap!=null){
										file = new File(cacheManager.getDiskCacheDir() + "/" + name);
										bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
										//与磁盘缓存保持一致
										if(bitmapInfo.isIcon()){
											bitmap = ImageUtils.decodeIcon(context, file.getAbsolutePath(), bitmapInfo.getDstWidth());
										}else{
											bitmap = ImageUtils.decodeFile(context, file.getAbsolutePath(), bitmapInfo.getDstWidth());
										}
									}
								} catch (FileNotFoundException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}else{
							bitmap = HttpUtils.getBitmap(path);
						}
					}
				}else if(BitmapInfo.TYPE_FTP.equals(bitmapInfo.getType())){
					if(bitmap==null){
						String name = "";
						int start = path.lastIndexOf("/");
						if(start!=-1){
							name = path.substring(start + 1);
						}
						if(cacheManager.getDiskCacheDir()!=null){//磁盘缓存可用
							File file = cacheManager.getCacheFile(name);
							if(file!=null){
								bitmap = ImageUtils.decodeFile(context, file.getAbsolutePath(), bitmapInfo.getDstWidth());
							}else{
//								FtpClient ftpClient = new FtpClient();
//								ftpClient.connect(Config.FTP_HOST, Config.FTP_PORT);
//								ftpClient.login(Config.FTP_USER, Config.FTP_PASSWORD);
//								InputStream is = ftpClient.getInputStream(path);
//								if(is!=null){
//									bitmap = BitmapFactory.decodeStream(is);
//									try {
//										if(bitmap!=null){
//											file = new File(cacheManager.getDiskCacheDir() + "/" + name);
//											bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
//										}
//									} catch (FileNotFoundException e) {
//										// TODO Auto-generated catch block
//										e.printStackTrace();
//									}
//								}
//								ftpClient.disconnect();
							}
						}else{
//							FtpClient ftpClient = new FtpClient();
//							ftpClient.connect(Config.FTP_HOST, Config.FTP_PORT);
//							ftpClient.login(Config.FTP_USER, Config.FTP_PASSWORD);
//							InputStream is = ftpClient.getInputStream(path);
//							if(is!=null){
//								bitmap = BitmapFactory.decodeStream(is);
//							}
//							ftpClient.disconnect();
						}
					}
				}
			}
		}
		return bitmap;
	}

	public BitmapInfo getBitmapInfo() {
		return bitmapInfo;
	}


}
